home *** CD-ROM | disk | FTP | other *** search
/ Aminet 19 / Aminet 19 (1997)(GTI - Schatztruhe)[!][Jun 1997].iso / Aminet / util / wb / Variator.lha / Variator.c < prev    next >
C/C++ Source or Header  |  1997-03-14  |  11KB  |  360 lines

  1. /*
  2.     $VER: Variator.c 2.0 (12.03.1997) Copyright 1991, 1997 by Dalibor Kezele
  3.  
  4.     First version: Monday, February 10, 1991
  5.     Last version: Wednesday, Mart 12, 1997
  6.  
  7.     Source code compiled with Aztec C 5.0a.
  8.     e-mail: dkezele@mia.os.carnet.hr
  9.  
  10. */
  11.  
  12. #include <intuition/intuition.h>
  13. #include <intuition/intuitionbase.h>
  14.  
  15. struct IntuitionBase *IntuitionBase;
  16. struct TextAttr t8f = { (UBYTE *)"topaz.font", 8, 0, 0 };
  17.  
  18. struct IntuiText
  19.     bit = { 1, 0, JAM1, -35, 0, &t8f, (UBYTE *)"BIN", NULL },
  20.     oit = { 1, 0, JAM1, -35, 0, &t8f, (UBYTE *)"OCT", NULL },
  21.     dit = { 1, 0, JAM1, -35, 0, &t8f, (UBYTE *)"DEC", NULL },
  22.     hit = { 1, 0, JAM1, -35, 0, &t8f, (UBYTE *)"HEX", NULL },
  23.     hgt = { 1, 0, JAM1, 16, 2, &t8f, (UBYTE *)"Help", NULL },
  24.     agt = { 1, 0, JAM1, 12, 2, &t8f, (UBYTE *)"About", NULL },
  25.     qgt = { 1, 0, JAM1, 16, 2, &t8f, (UBYTE *)"Quit", NULL },
  26.     hait[13],
  27.     hagt = { 1, 0, JAM1, 23, 2, &t8f, (UBYTE *)"OK", NULL };
  28.  
  29. UBYTE
  30.     bms[80] = "", bus[80] = "", oms[80] = "", ous[80] = "",
  31.     dms[80] = "", dus[80] = "", hms[80] = "", hus[80] = "";
  32.  
  33. struct StringInfo
  34.     bsi = { bms, bus, 0, 33, 0, 0, 33, 0, 0, 0, NULL, 0, NULL },
  35.     osi = { oms, ous, 0, 12, 0, 0, 12, 0, 0, 0, NULL, 0, NULL },
  36.     dsi = { dms, dus, 0, 11, 0, 0, 11, 0, 0, 0, NULL, 0, NULL },
  37.     hsi = { hms, hus, 0, 9, 0, 0, 9, 0, 0, 0, NULL, 0, NULL };
  38.  
  39. short int
  40.     bc[10] = { 0, 0, 269, 0, 269, 11, 0, 11, 0, 0 },
  41.     oc[10] = { 0, 0, 101, 0, 101, 11, 0, 11, 0, 0 },
  42.     dc[10] = { 0, 0, 93, 0, 93, 11, 0, 11, 0, 0 },
  43.     hc[10] = { 0, 0, 77, 0, 77, 11, 0, 11, 0, 0 },
  44.     qc1[6] = { 0, 10, 0, 0, 65, 0 },
  45.     qc[6] = { 65, 0, 65, 10, 0, 10 },
  46.     tc[13] = { 0, 8, 16, 24, 34, 42, 50, 58, 66, 76, 84, 92, 102 },
  47.     hagc1[6] = { 0, 10, 0, 0, 61, 0 },
  48.     hagc[6] = { 61, 0, 61, 10, 0, 10 };
  49.  
  50. int
  51.     htc[13] = { 1, 1, 1, 1, 1, 3, 3, 3, 3, 1, 1, 1, 1 },
  52.     atc[13] = { 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1 };
  53.  
  54. struct Border
  55.     bb = { -3, -2, 3, 0, JAM1, 5, (short *)&bc, NULL },
  56.     ob = { -3, -2, 3, 0, JAM1, 5, (short *)&oc, NULL },
  57.     db = { -3, -2, 3, 0, JAM1, 5, (short *)&dc, NULL },
  58.     hb = { -3, -2, 3, 0, JAM1, 5, (short *)&hc, NULL },
  59.     qb1 = { 0, 0, 2, 0, JAM1, 3, (short *)&qc1, NULL },
  60.     qb = { 0, 0, 1, 0, JAM1, 3, (short *)&qc, (struct Border *)&qb1 },
  61.     hgb1 = { 0, 0, 2, 0, JAM1, 3, (short *)&hagc1, NULL },
  62.     hgb = { 0, 0, 1, 0, JAM1, 3, (short *)&hagc, (struct Border *)&hgb1 };
  63.  
  64.  
  65. struct Gadget
  66.     bg = { (struct Gadget *)NULL, 50, 16, 267, 11, GADGHCOMP, RELVERIFY,
  67.         STRGADGET, (APTR)&bb, NULL, &bit, NULL, (APTR)&bsi, 1, NULL },
  68.     og = { (struct Gadget *)&bg, 50, 29, 98, 11, GADGHCOMP, RELVERIFY,
  69.         STRGADGET, (APTR)&ob, NULL, &oit, NULL, (APTR)&osi, 2, NULL },
  70.     dg = { (struct Gadget *)&og, 50, 42, 92, 11, GADGHCOMP, LONGINT|RELVERIFY,
  71.         STRGADGET, (APTR)&db, NULL, &dit, NULL, (APTR)&dsi, 3, NULL },
  72.     hg = { (struct Gadget *)&dg, 50, 55, 76, 11, GADGHCOMP, RELVERIFY,
  73.         STRGADGET, (APTR)&hb, NULL, &hit, NULL, (APTR)&hsi, 4, NULL },
  74.     eg = { (struct Gadget *)&hg, 200, 28, 66, 11, GADGHCOMP, GADGIMMEDIATE|RELVERIFY,
  75.         BOOLGADGET, (APTR)&qb, NULL, &hgt, NULL, NULL, 5, NULL },
  76.     ag = { (struct Gadget *)&eg, 200, 41, 66, 11, GADGHCOMP, GADGIMMEDIATE|RELVERIFY,
  77.         BOOLGADGET, (APTR)&qb, NULL, &agt, NULL, NULL, 6, NULL },
  78.     qg = { (struct Gadget *)&ag, 200, 54, 66, 11, GADGHCOMP, GADGIMMEDIATE|RELVERIFY,
  79.         BOOLGADGET, (APTR)&qb, NULL, &qgt, NULL, NULL, 7, NULL },
  80.     hgg = { NULL, 189, 128, 62, 11, GADGHCOMP, GADGIMMEDIATE|RELVERIFY,
  81.         BOOLGADGET, (APTR)&hgb, NULL, &hagt, NULL, NULL, 1, NULL },
  82.     *gd;
  83.  
  84. struct Window *mw, *hw;
  85.  
  86. struct NewWindow
  87.     mnw = { 0, 0, 334, 70, 0, 1, CLOSEWINDOW|GADGETUP|VANILLAKEY,
  88.         WINDOWDRAG|WINDOWCLOSE|WINDOWDEPTH|ACTIVATE|NOCAREREFRESH|SMART_REFRESH,
  89.         (struct Gadget *)&qg, NULL, (UBYTE *)"Variator", NULL, NULL, 0, 0, 0, 0, CUSTOMSCREEN },
  90.     nhw ={ 0, 0, 434, 142 , 0, 3, GADGETUP|VANILLAKEY,
  91.         ACTIVATE|SMART_REFRESH|NOCAREREFRESH|WINDOWDRAG,
  92.         (struct Gadget *)&hgg, NULL, (UBYTE *)"", NULL, NULL, 0, 0, 0, 0, CUSTOMSCREEN};
  93.  
  94. struct IntuiMessage *mm, *hm;
  95. struct Screen *wb;
  96.  
  97. unsigned char
  98.     version[] = "$VER: Variator 2.0 (12.03.1997) Copyright 1991, 1997 by Dalibor Kezele",
  99.     chars[17] = "0123456789ABCDEF",
  100.     HelpTitle[16] = "Variator Help",
  101.     AboutTitle[16] = "About Variator",
  102.     error[6] = "Error",
  103.  
  104.     ht[13][53] = {
  105.         "   Variator is very simply to use. You only need to",
  106.         "click on one of BIN, OCT, DEC and HEX gadgets, enter",
  107.         "a number and press <CR>, and you number is convetted",
  108.         "to other bases in a speed of light.",
  109.         "   Keyboard shortcuts:",
  110.         "        <B> == Bin        <C>, <DEL> == Clear",
  111.         "        <O> == Oct        <?>, <CR>  == Help",
  112.         "        <D> == Dec        <!>, <A>   == About",
  113.         "        <H> == Hex        <Q>, <ESC> == Exit",
  114.         "   Numbers are 32-bit long integers. If you enter an",
  115.         "illegal character (like '2' in a binary number) or",
  116.         "a number higher than 32-bit an error is displayed.",
  117.         "   Well, why don't you click the \"about\" gadget?",
  118.         },
  119.  
  120.     at[13][53] = {
  121.         "   Since I started working with Amiga I haven't seen",
  122.         "any program which just recalculates numbers from one",
  123.         "base to another. So I made it. And it helps me now a",
  124.         "lot. I would like you to use it.",
  125.         "   This program is POSTCARDWARE. If you like it and",
  126.         "use it, please send me a postcard. My address is:",
  127.       "",
  128.         "                 Dalibor S. Kezele",
  129.         "                   Toplicka 127",
  130.         "                   42204 Turcin",
  131.         "                     Croatia",
  132.       "",
  133.         "     (Check Variator.DOC for more information!)     ",
  134.         };
  135.  
  136. /* -------------------------------------------------------------------- */
  137.  
  138. int main(void)
  139. {
  140. ULONG class;
  141. USHORT code;
  142. unsigned long longbuffer;
  143.  
  144. if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L)))
  145.     return 0;
  146.  
  147. wb = IntuitionBase -> ActiveScreen;
  148. mnw.LeftEdge = ((wb -> Width) - (mnw.Width)) / 2;
  149. mnw.Screen = (struct Screen *) wb;
  150.  
  151. nhw.TopEdge = ((wb -> Height) - (nhw.Height)) / 2;
  152. nhw.LeftEdge = ((wb -> Width) - (nhw.Width)) / 2;
  153. nhw.Screen = (struct Screen *) wb;
  154.  
  155. if(!(mw = (struct Window *)OpenWindow(&mnw))) {
  156.     CloseLibrary(IntuitionBase);
  157.     return 0;
  158.     }
  159.  
  160. while(1) {
  161.     Delay(8L);
  162.     mm = (struct IntuiMessage *)GetMsg(mw->UserPort);
  163.     class = mm -> Class;
  164.     code = mm -> Code;
  165.  
  166.     if(mm == NULL) continue;
  167.  
  168.     if(class == CLOSEWINDOW)
  169.         break;
  170.  
  171.     if(class == GADGETUP) {
  172.         gd = (struct Gadget *) mm -> IAddress;
  173.         if(gd -> GadgetID == 7)
  174.             break;
  175.         else
  176.             switch(gd -> GadgetID) {
  177.                 case 1 :
  178.                     if(any2dec(&longbuffer, 2, (char *)bms)) {
  179.                         strcpy(oms, &error);
  180.                         strcpy(dms, &error);
  181.                         strcpy(hms, &error);
  182.                         }
  183.                     else {
  184.                         dec2any(longbuffer, 8, (char *)oms);
  185.                         dec2any(longbuffer, 10, (char *)dms);
  186.                         dec2any(longbuffer, 16, (char *)hms);
  187.                     }
  188.                     RefreshGList(&hg, mw, NULL, 4L);
  189.                     break;
  190.                 case 2 :
  191.                     if((any2dec(&longbuffer, 8, (char *)oms))
  192.                         || (strlen(oms)==11 && oms[0]>'3')) {
  193.                             strcpy(bms, &error);
  194.                             strcpy(dms, &error);
  195.                             strcpy(hms, &error);
  196.                         }
  197.                     else {
  198.                         dec2any(longbuffer, 2, (char *)bms);
  199.                         dec2any(longbuffer, 10, (char *)dms);
  200.                         dec2any(longbuffer, 16, (char *)hms);
  201.                     }
  202.                     RefreshGList(&hg, mw, NULL, 4L);
  203.                     break;
  204.                 case 3 :
  205.                     dec2any(dsi.LongInt, 2, (char *)bms);
  206.                     dec2any(dsi.LongInt, 8, (char *)oms);
  207.                     dec2any(dsi.LongInt, 10, (char *)dms);
  208.                     dec2any(dsi.LongInt, 16, (char *)hms);
  209.                     RefreshGList(&hg, mw, NULL, 4L);
  210.                     break;
  211.                 case 4 :
  212.                     if(any2dec(&longbuffer, 16, (char *)hms)) {
  213.                         strcpy(bms, &error);
  214.                         strcpy(oms, &error);
  215.                         strcpy(dms, &error);
  216.                         }
  217.                     else {
  218.                         dec2any(longbuffer, 2, (char *)bms);
  219.                         dec2any(longbuffer, 8, (char *)oms);
  220.                         dec2any(longbuffer, 10, (char *)dms);
  221.                     }
  222.                     RefreshGList(&hg, mw, NULL, 4L);
  223.                     break;
  224.                 case 5 :
  225.                     helpabout(0);
  226.                     break;
  227.                 case 6 :
  228.                     helpabout(1);
  229.                     break;
  230.                 }
  231.         }
  232.     if(class == VANILLAKEY) {
  233.         if(code == 27 || code == 'q' || code == 'Q')
  234.             break;
  235.         else
  236.             switch(code) {
  237.                 case 'b' :
  238.                 case 'B' : ActivateGadget(&bg, mw, NULL); break;
  239.                 case 'o' :
  240.                 case 'O' : ActivateGadget(&og, mw, NULL); break;
  241.                 case 'd' :
  242.                 case 'D' : ActivateGadget(&dg, mw, NULL); break;
  243.                 case 'h' :
  244.                 case 'H' : ActivateGadget(&hg, mw, NULL); break;
  245.                 case 13 :
  246.                 case '?' : helpabout(0); break;
  247.                 case 'a' :
  248.                 case 'A' :
  249.                 case '!' : helpabout(1); break;
  250.                 case 127 :
  251.                 case 'c' :
  252.                 case 'C' : bms[0] = oms[0] = dms[0] = hms[0] = 0;
  253.                     RefreshGList(&hg, mw, NULL, 4L);;
  254.             }
  255.         }
  256.  
  257.     ReplyMsg(mm);
  258.     }
  259.  
  260. CloseWindow(mw);
  261. CloseLibrary(IntuitionBase);
  262. return 0;
  263. }
  264.  
  265. /* -------------------------------------------------------------------- */
  266.  
  267. int dec2any(unsigned long decimal, int base, char *newnumber)
  268. {
  269.     unsigned long number;
  270.     int helpnumber[40];
  271.     int letter, count, max = 0;
  272.  
  273.     while(decimal) {
  274.         number = decimal / base;
  275.         letter = decimal - (number * base);
  276.         helpnumber [++max] = letter;
  277.         if(max > 39) return 1;
  278.         decimal = number;
  279.         }
  280.     for(count=max; count >=1; count--)
  281.         newnumber[max-count] = chars[helpnumber[count]];
  282.  
  283.     newnumber[max] = 0;
  284.     return 0;
  285. }
  286.  
  287. /* -------------------------------------------------------------------- */
  288.  
  289. int any2dec(unsigned long *decimal, int base, char *number)
  290. {
  291.     unsigned long countbase = 1;
  292.     int count, letter;
  293.     char onechar;
  294.  
  295.     *decimal = 0;
  296.     for(count = strlen(number) - 1; count >= 0; count--) {
  297.         onechar = toupper(number[count]);
  298.         if(onechar < 65)
  299.             letter = onechar - 48;
  300.         else
  301.             letter = onechar - 55;
  302.         if(letter < 0 || letter >= base) return 1;
  303.         *decimal = *decimal + letter * countbase;
  304.         countbase *= base;
  305.         }
  306.     return 0;
  307. }
  308.  
  309. /* -------------------------------------------------------------------- */
  310.  
  311. int helpabout(short choice)
  312. {
  313. ULONG klass;
  314. USHORT kode;
  315. register short counter = 0;
  316.  
  317. while(counter<13) {
  318.     if(choice) {
  319.         hait[counter].FrontPen = atc[counter];
  320.         hait[counter].IText = (UBYTE *)&at[counter][0];
  321.         }
  322.     else {
  323.         hait[counter].FrontPen = htc[counter];
  324.         hait[counter].IText = (UBYTE *)&ht[counter][0];
  325.     }
  326.     hait[counter].BackPen = 0;
  327.     hait[counter].DrawMode = JAM1;
  328.     hait[counter].LeftEdge = 0;
  329.     hait[counter].TopEdge = tc[counter];
  330.     hait[counter].ITextFont = &t8f;
  331.     hait[counter++].NextText = NULL;
  332.     }
  333.  
  334. if(choice)
  335.     nhw.Title = (UBYTE *)&AboutTitle;
  336. else
  337.     nhw.Title = (UBYTE *)&HelpTitle;
  338.  
  339. if(!(hw = (struct Window *)OpenWindow(&nhw))) return 1;
  340.  
  341. counter = 0;
  342.  
  343. while(counter<13)
  344.     PrintIText(hw -> RPort, &hait[counter++], 9, 15);
  345.  
  346. while(1) {
  347.     Delay(8L);
  348.     hm = (struct IntuiMessage *)GetMsg(hw->UserPort);
  349.     if(hm == NULL) continue;
  350.     klass = hm -> Class;
  351.     kode = hm -> Code;
  352.     if(klass == GADGETUP || (klass == VANILLAKEY && (kode == 27 || kode == 13)))
  353.             break;
  354.     }
  355.  
  356. ReplyMsg(hm);
  357. CloseWindow(hw);
  358. return 0;
  359. }
  360.